const express = require("express");
const bodyParser = require("body-parser");
const request = require("request");
const app = express();
var testUrl = "http://120.78.65.26:8889/api";
app.use(function (req, res, next) {
    //开放权限,允许跨域,设置请求头，允许DELETE方法
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers",["token","version"]);
    res.header("Access-Control-Allow-Methods",["DELETE","PUT"]);
    next();
});
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//首页
app.get('/getindex', function (req, res) {
    var url = `${testUrl}/index`;
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//关键字搜索
app.get('/getkeyword', function (req, res) {
    var url = `${testUrl}/index/keyword`;
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//关键字搜索结果
app.get('/getkeywordresult', function (req, res) {
    var url = `${testUrl}/product/list?keyword=${req.query.keyword}`;
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//积分专区
app.get('/getpoint', function (req, res) {
    var url = "${testUrl}/custom";
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    })
});
//商品分类
app.get('/getcategory', function (req, res) {
    var url = "${testUrl}/category";
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    })
});
//分类商品
app.get('/getcategoryproduct', function (req, res) {
    var url = `${testUrl}/product/list?categoryId=${req.query.categoryId}&pageNum=${req.query.pageNum}&sort=${req.query.sort}`;
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    })
});
//活动
app.get('/getactivity', function (req, res) {
    var url = "${testUrl}/activity/list";
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    })
});
//获取活动详情
app.get('/getactivitydetail', function (req, res) {
    var url = `${testUrl}/activity?id=${req.query.id}`;
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//获取活动评论
app.get('/getactivitycomments', function (req, res) {
    var url = `${testUrl}/activity/review?id=${req.query.id}&pageNum=${req.query.pageNum}`;
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//猜你喜欢
app.get('/getlike', function (req, res) {
    var url = `${testUrl}/index/like?pageNum=${req.query.pageNum}`;
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    })
});
//获取商品详情信息
app.get('/getproductdetail', function (req, res) {
    var url = `${testUrl}/product?id=${req.query.id}`
    request({
        url,
        headers:{
            "token":req.headers.token
        },
    }, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    })
});
//获取商品sku
app.get('/getproductsku', function (req, res) {
    var url = `${testUrl}/product/sku?id=${req.query.id}`
    request({
        url,
    }, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    })
});
//加入购物车
app.post('/addcar', function (req, res) {
    console.log(req.body);
    console.log(req.headers.token);
    console.log(req.headers.version);
    var url = `${testUrl}/cart/add`;
    request.post(
        {
            url,
            headers:{
                "token":req.headers.token,
                "version":req.headers.version
            },
            form:req.body
        },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//获取商品评论数
app.get('/getproductcommentsnum', function (req, res) {
    var url = `${testUrl}/product/review/number?id=${req.query.id}`
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    })
});
//获取所有评论
app.get('/getproductallcomments', function (req, res) {
    var url = `${testUrl}/product/review/list?id=${req.query.id}`
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    })
});
//获取商品好评数
app.get('/getproductgoodcomments', function (req, res) {
    var url = `${testUrl}/product/review/list?id=${req.query.id}&score=${req.query.score}&pageNum=${req.query.pageNum}`
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    })
});
//获取商品中评数
app.get('/getproductneutralcomments', function (req, res) {
    var url = `${testUrl}/product/review/list?id=${req.query.id}&score=${req.query.score}&pageNum=${req.query.pageNum}`
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    })
});
//获取商品差评数
app.get('/getproductbadcomments', function (req, res) {
    var url = `${testUrl}/product/review/list?id=${req.query.id}&score=${req.query.score}&pageNum=${req.query.pageNum}`
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    })
});
//获取主题
app.get('/gettheme', function (req, res) {
    var url = `${testUrl}/theme?id=${req.query.id}`;
    request(url, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//获取推文
app.get('/gettweet', function (req, res) {
    var url = `${testUrl}/content?id=${req.query.id}`;
    request({
        url,
        headers:{
            "token":req.headers.token
        }
    }, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//推文点赞
app.get('/thumbtweet', function (req, res) {
    var url = `${testUrl}/content/love?id=${req.query.id}`;
    request({
        url,
        headers:{
            "token":req.headers.token
        }
    }, function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//登录
app.post('/getlogin', function (req, res) {
    var url = `${testUrl}/login`;
    request.post(
        {
            url,
            form:req.body
        },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//个人主页
app.get('/getme', function (req, res) {
    var url = `${testUrl}/member/index`;
    request(
        {
            url,
            headers:{
                "token":req.headers.token
            }
    },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//收藏列表
app.get('/getcollection', function (req, res) {
    var url = `${testUrl}/love/list`;
    request({url,headers:{
        "token":req.headers.token
    }},function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//删除收藏商品
app.delete('/delcollection', function (req, res) {
    var url = `${testUrl}/love/del?id=${req.query.id}`;
    request.delete(
        {
            url,
            headers:{
                "token":req.headers.token
            },
    },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//获取地址列表
app.get('/getaddresslist', function (req, res) {
    var url = `${testUrl}/address/list`;
    request(
        {
            url,
            headers:{
                "token":req.headers.token
            },
    },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//地址
app.post('/address', function (req, res) {
    console.log(req.body);
    var url = `${testUrl}/address/save`;
    request.post(
        {
            url,
            headers:{
                "token":req.headers.token
            },
            form:req.body
        },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//删除地址
app.delete('/deladdress', function (req, res) {
    console.log(req.query.id);
    var url = `${testUrl}/address/del?id=${req.query.id}`;
    request.delete(
        {
            url,
            headers:{
                "token":req.headers.token
            },
            form:req.body
        },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//获取购物车列表
app.get('/getcarlist', function (req, res) {
    var url = `${testUrl}/cart`;
    request(
        {
            url,
            headers:{
                "token":req.headers.token
            },
    },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//删出购物车
app.delete('/delcar', function (req, res) {
    console.log(req.query.id);
    var url = `${testUrl}/cart/del?id=${req.query.id}`;
    request.delete(
        {
            url,
            headers:{
                "token":req.headers.token
            },
            form:req.body
        },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//购物车商品数量增减
app.put('/addorcutcar', function (req, res) {
    console.log(req.body);
    var url = `${testUrl}/cart/updCount`;
    request.put(
        {
            url,
            headers:{
                "token":req.headers.token
            },
            form:req.body
        },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//购物车结算
app.get('/carcalculate', function (req, res) {
    var url = `${testUrl}/cart/confirm`;
    request(
        {
            url,
            headers:{
                "token":req.headers.token
            },
    },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//获取订单列表
app.get('/getorderlist', function (req, res) {
    var url = `${testUrl}/order/list?pageNum=${req.query.pageNum}&status=${req.query.status}`;
    request(
        {
            url,
            headers:{
                "token":req.headers.token
            },
    },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//获取订单详情
app.get('/getorderdetail', function (req, res) {
    if(req.query.orderId) {
        var url = `${testUrl}/order?orderId=${req.query.orderId}`;
    }
    if(req.query.orderSkuId) {
        var url = `${testUrl}/order?orderSkuId=${req.query.orderSkuId}`;
    }
    request(
        {
            url,
            headers:{
                "token":req.headers.token
            },
    },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//取消订单
app.get('/cancleorder', function (req, res) {
   var url = `${testUrl}/order/cancel?id=${req.query.id}`
    request(
        {
            url,
            headers:{
                "token":req.headers.token
            },
    },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//获取消息列表
app.get('/getmsg', function (req, res) {
    var url = `${testUrl}/msg`;
    request(
        {
            url,
            headers:{
                "token":req.headers.token
            },
    },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//删除消息
app.get('/delmsg', function (req, res) {
    var url = `${testUrl}/msg/del?id=${req.query.id}`;
    request(
        {
            url,
            headers:{
                "token":req.headers.token
            },
    },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//获取省市县
app.get('/getssx', function (req, res) {
    var url = `${testUrl}/region/list?pid=${req.query.pid}`;
    request(
        {
            url,
            headers:{
                "token":req.headers.token
            },
    },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//获取手机验证码
app.post('/gettelcode', function (req, res) {
    console.log(req.body);
    var url = `${testUrl}/captcha`;
    request.post(
        {
            url,
            headers:{
                "token":req.headers.token
            },
            form:req.body
        },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//重置登录密码
app.post('/resetpsd', function (req, res) {
    console.log(req.body);
    var url = `${testUrl}/resetPwd`;
    request.post(
        {
            url,
            // headers:{
            //     "token":req.headers.token
            // },
            form:req.body
        },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//修改支付密码
app.post('/resetpaypsd', function (req, res) {
    console.log(req.body);
    var url = `${testUrl}/member/payPwd`;
    request.post(
        {
            url,
            headers:{
                "token":req.headers.token
            },
            form:req.body
        },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//用户注册
app.post('/reg', function (req, res) {
    console.log(req.body);
    var url = `${testUrl}/reg`;
    request.post(
        {
            url,
            // headers:{
            //     "token":req.headers.token
            // },
            form:req.body
        },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//意见反馈
app.post('/feedback', function (req, res) {
    console.log(req.body);
    var url = `${testUrl}/feedback`;
    request.post(
        {
            url,
            headers:{
                "token":req.headers.token
            },
            form:req.body
        },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//绑定手机
app.post('/bindnewphone', function (req, res) {
    console.log(req.body);
    var url = `${testUrl}/member/bindMobile`;
    request.post(
        {
            url,
            headers:{
                "token":req.headers.token
            },
            form:req.body
        },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//上传头像
app.post('/uploadheadpic', function (req, res) {
    var url = `${testUrl}/member/face`;
    request.post(
        {
            url,
            headers:{
                "token":req.headers.token
            },
            form:req.body
        },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//修改资料
app.post('/editmsg', function (req, res) {
    var url = `${testUrl}/member/save`;
    request.post(
        {
            url,
            headers:{
                "token":req.headers.token
            },
            form:req.body
        },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//提交订单
app.post('/submitorder', function (req, res) {
    var url = `${testUrl}/cart/create`;
    console.log(req.body);
    request.post(
        {
            url,
            headers:{
                "token":req.headers.token,
                "version":req.headers.version
            },
            form:req.body
        },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//微信支付
app.post('/wxpay', function (req, res) {
    var url = `${testUrl}/wxpay/payBefore`;
    console.log(req.body);
    request.post(
        {
            url,
            headers:{
                "token":req.headers.token,
                "version":req.headers.version
            },
            form:req.body
        },function (err, response, body) {
        res.send({ data: JSON.parse(body.toString()) });
    });
});
//启动服务器
app.listen(3005, function () {
    console.log('start at http://127.0.0.1:3005');
});